package com.topboya.linker.ui.activity;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.topboya.linker.R;
import com.topboya.linker.model.levelitem.DepartmentGroupBo;
import com.topboya.linker.model.levelitem.SelectDeviceGroup;
import com.topboya.linker.model.response.CustomerByIdResponse;
import com.topboya.linker.model.response.DictByKeyResponse;
import com.topboya.linker.model.response.OrganizationResopnse;
import com.topboya.linker.model.response.TitleDictResopnse;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.presenter.AddCustomerPresenter;
import com.topboya.linker.ui.view.AddCustomerView;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

public class AddCustomerActivity extends BaseActivity<AddCustomerView, AddCustomerPresenter> implements AddCustomerView {
    private static final String TAG = "AddCustomerActivity:";
    private ScrollView scroll;
    private TextView toolbar_title;
    private TextView tv_toolbar_right;
    private Toolbar toolbar;
    private EditText et_add_name;
    private EditText et_add_phone;
    private TextView tv_select_dapartment;
    private TextView tv_select_professional;
    private TextView et_add_address;
    private List<DictByKeyResponse.Data> listDepartment;
    private List<TitleDictResopnse.Data> listTitle;
    private List<String> listDepartmentname;
    private List<String> listTitlename;
    private Button btn_save;
    private SelectDeviceGroup  docDetailInfo;
    private String orgID;
    private String departmentvalue ;
    private String posit;
    private CustomerByIdResponse.Data data;
    Handler h = new Handler(){
        public void handleMessage(Message msg) {
        };
    };
    /**
     * 使ScrollView指向底部
     */
    private void changeScrollView(){

        h.postDelayed(() -> scroll.scrollTo(0, scroll.getHeight()+100), 300);
    }


    @Override
    protected int getLayoutId() {

        return R.layout.activity_add_customer;
    }

    @Override
    protected void initView() {

        scroll = findViewById(R.id.scroll);
        toolbar_title = findViewById(R.id.toolbar_title);
        tv_toolbar_right = findViewById(R.id.tv_toolbar_right);
        toolbar = findViewById(R.id.toolbar);
        et_add_name = findViewById(R.id.et_add_name);
        et_add_phone = findViewById(R.id.et_add_phone);
        et_add_address = findViewById(R.id.et_add_address);
        tv_select_dapartment = findViewById(R.id.tv_select_dapartment);
        tv_select_professional = findViewById(R.id.tv_select_professional);
        btn_save = findViewById(R.id.btn_save);
        tv_toolbar_right.setVisibility(View.GONE);
    }

    @Override
    protected void initData() {

        listDepartment = new ArrayList<>();
        listTitle = new ArrayList<>();
        docDetailInfo = (SelectDeviceGroup) getIntent().getSerializableExtra("docDetailInfo");
        data = (CustomerByIdResponse.Data) getIntent().getSerializableExtra("data");
        orgID = docDetailInfo.getId();
        LogUtils.v(TAG +"docDetailInfo:"+docDetailInfo.toString());

        if (null == data){

            toolbar_title.setText("新增客户");
            btn_save.setText("保存");

        }else {

            toolbar_title.setText("修改客户");
            btn_save.setText("修改");

        }
//        首先拿到字典数据科室和职称
        mPresenter.getDictByKey("hos_department");

    }

    @Override
    protected void initListener() {
        et_add_address.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus){

                changeScrollView();
            }
        });
        toolbar.setOnClickListener(v -> {

//            back();
            finish();
        });
        tv_select_dapartment.setOnClickListener(v -> {
            //条件选择器
            OptionsPickerView pvOptions = new OptionsPickerBuilder(this, (options1, option2, options3, v1) -> {

                String departmentname = listDepartmentname.get(options1);

                 departmentvalue = listDepartment.get(options1).getValue();
                tv_select_dapartment.setText(departmentname);

            }).setCyclic(true,true,true)
              .setTextColorCenter(Color.BLUE)
              .build();
            pvOptions.setPicker(listDepartmentname , null, null);
            pvOptions.show();

        });
        tv_select_professional.setOnClickListener(v -> {

            //条件选择器
            OptionsPickerView pvOptions = new OptionsPickerBuilder(this, (options1, option2, options3, v1) -> {

                String titlementname = listTitlename.get(options1);
                posit = listTitle.get(options1).getValue();
                tv_select_professional.setText(titlementname);

            }).setCyclic(true,true,true)
              .setTextColorCenter(Color.BLUE)
              .build();

            pvOptions.setPicker(listTitlename, null, null);
            pvOptions.show();

        });

        btn_save.setOnClickListener(v -> {

           String cusName = et_add_name.getText().toString().trim();
           String phone = et_add_phone.getText().toString().trim();
           String address =et_add_address.getText().toString().trim();
           if (TextUtils.isEmpty(cusName)){
               UIUtils.showToast("请先输入客户姓名");
               return;
           }
           if (TextUtils.isEmpty(departmentvalue)){

               UIUtils.showToast("请先选择科室");
               return;
           }
           if (TextUtils.isEmpty(phone)){

               UIUtils.showToast("请先输入电话号码");
               return;
           }
           if (TextUtils.isEmpty(posit)){

               UIUtils.showToast("请先选择职称");
               return;
           }
           if (TextUtils.isEmpty(address)){

               UIUtils.showToast("请先输入地址");
               return;
           }
            mPresenter.createDoctorCustomer(orgID,null,cusName,phone,departmentvalue,posit,address);

        });

    }

    @Override
    protected AddCustomerPresenter createPresenter() {

        return new AddCustomerPresenter(this);
    }


    @Override
    public void setPickViewData(List<DictByKeyResponse.Data> listDepartment, List<TitleDictResopnse.Data> listTitle) {

        LogUtils.v(TAG + "listDepartment:"+listDepartment.toString());
        LogUtils.v(TAG + "listTitle:"+listTitle.toString());
        this.listDepartment.clear();
        this.listDepartment.addAll(listDepartment);
        this.listTitle.clear();
        this.listTitle.addAll(listTitle);
        listDepartmentname = new ArrayList<>();
        listTitlename = new ArrayList<>();
        listDepartmentname.clear();
        listTitlename.clear();





        for (DictByKeyResponse.Data listDepartment1:listDepartment) {

            listDepartmentname.add(listDepartment1.getLabel());
        }

        for (TitleDictResopnse.Data listTitle1:listTitle) {

            listTitlename.add(listTitle1.getLabel());
        }
    }

    @Override
    public void createCustomer() {

        LogUtils.v(TAG +"执行成功,跳转到任务列表页面");
        UIUtils.showToast("添加成功");
        back();

    }



//    @Override
//    public void onBackPressed() {
//
//        back();
//    }
//
//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        // 是否触发按键为back键
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            onBackPressed();
//            return true;
//        } else { // 如果不是back键正常响应
//            return super.onKeyDown(keyCode, event);
//        }
//    }

    private void back() {

        setResult(1003);
        finish();
    }
}
